package com.datamining.online

import java.util.Properties

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession

/**
  * Created by Administrator on 2017/5/9.
  */
/**
  * spark-test
  * MysqlTest
  *
  * @author Administrator kevin
  * @create 2017-05-09 18:55
  */
object MysqlTest {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf();
    sparkConf.setMaster("yarn"); // 本地模式
    sparkConf.setAppName("my_test");

    val jdbc_url = "jdbc:mysql://192.168.20.234:3306/Yamibuy_Master"
    val table_name = "xysc_order_info"

    //    val sc = new SparkContext(sparkConf)
    // 创建sparkSession
    val sparkSession = SparkSession.builder().appName("mysqlTest").config(conf = sparkConf).getOrCreate();

    val properties = new Properties()
    properties.setProperty("user", "root")
    properties.setProperty("password", "root")
    properties.setProperty("driver", "com.mysql.jdbc.Driver")

    //读
    val df = sparkSession.sqlContext.read.jdbc(jdbc_url, table_name, properties)
    df.select("order_id", "order_sn", "user_id").limit(10).show()

    // 写
    //    sparkSession.createDataFrame(ttt_rdd_sort_sub).write.mode(SaveMode.Overwrite).jdbc(jdbc_url, table_name, properties)
  }

}
